﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;

namespace RestAndPlay.Player.Player
{
    public class MediaPlayer
    {
        #region Singleton

        private static volatile MediaPlayer mediaPlayer;
        private static object mediaPlayerSyncRoot = new Object();

        public static MediaPlayer Instance
        {
            get
            {
                if (mediaPlayer == null)
                {
                    lock (mediaPlayerSyncRoot)
                    {
                        if (mediaPlayer == null)
                            mediaPlayer = new MediaPlayer();
                    }
                }

                return mediaPlayer;
            }
        }

        private MediaPlayer()
        {
        }

        #endregion

        const float VolumenStep = 0.05f;

        IWavePlayer waveOutDevice = new WaveOut();
        WaveStream mainOutputStream;
        WaveChannel32 volumeStream;

        public void Play()
        {
            waveOutDevice.Play();
        }

        public void Play(string path)
        {
            mainOutputStream = CreateInputStream(path);
            waveOutDevice.Init(mainOutputStream);
            waveOutDevice.Play();
        }

        public void Stop()
        {
            waveOutDevice.Stop();
        }

        public void Pause()
        {
            waveOutDevice.Pause();
        }

        public double GetVolume()
        {
            return waveOutDevice.Volume;
        }

        public void VolumeUp()
        {
            waveOutDevice.Volume += VolumenStep;
        }

        public void VolumeDown()
        {
            waveOutDevice.Volume -= VolumenStep;
        }


        private WaveStream CreateInputStream(string fileName)
        {
            WaveChannel32 inputStream;
            if (fileName.EndsWith(".mp3"))
            {
                WaveStream mp3Reader = new Mp3FileReader(fileName);
                inputStream = new WaveChannel32(mp3Reader);
            }
            else
            {
                throw new InvalidOperationException("Unsupported extension");
            }
            volumeStream = inputStream;
            return volumeStream;
        }

        private void CloseWaveOut()
        {
            if (waveOutDevice != null)
            {
                waveOutDevice.Stop();
            }
            if (mainOutputStream != null)
            {
                // this one really closes the file and ACM conversion
                volumeStream.Close();
                volumeStream = null;
                // this one does the metering stream
                mainOutputStream.Close();
                mainOutputStream = null;
            }
            if (waveOutDevice != null)
            {
                waveOutDevice.Dispose();
                waveOutDevice = null;
            }
        }
    }
}
